package com.ning.activitys.skysound.fragment.musicpavilion;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
import com.ning.activitys.skysound.R;
import com.ning.activitys.skysound.activity.MainActivity;
import com.ning.activitys.skysound.adpter.RankingAdapter;
import com.ning.activitys.skysound.base.BaseFragment;
import com.ning.activitys.skysound.base.MyApplication;
import com.ning.activitys.skysound.beans.GsonBeansRanking;
import com.ning.activitys.skysound.beans.NativeInformationBeans;
import com.ning.activitys.skysound.service.PlayService;
import com.ning.activitys.skysound.volley.VolleySingleton;

/**
 * Created by dllo on 16/1/8.
 */
public class RankingListFragment extends BaseFragment implements AdapterView.OnItemClickListener, View.OnClickListener {

    private String url = "http://a.vip.migu.cn/rdp2/v5.5/ranklist.do?groupcode=rank&ua=Iphone_Sst&version=4.243&pageno=1";

    private ListView listView;
    private RankingAdapter adapter;
    private MainActivity mainActivity;
    private Bundle bundle = new Bundle();
    private Gson gson = new Gson();

    private GsonBeansRanking gsonBeansRanking;

    private NativeInformationBeans nativeInformation = new NativeInformationBeans();
    private MyApplication myApplication;

    //url 的最后六位
    private String urls[] = {
            "http://218.200.160.29/rdp2/v5.5/rankinfo.do?groupcode=365905/365918/469202/469231&pageno=1&ua=Android_sst&version=4.2280",
            "http://218.200.160.29/rdp2/v5.5/rankinfo.do?groupcode=365905/365918/469202/471150&pageno=1&ua=Android_sst&version=4.2280",
            "http://218.200.160.29/rdp2/v5.5/rankinfo.do?groupcode=365905/365918/469202/472731&pageno=1&ua=Android_sst&version=4.2280",
            "http://218.200.160.29/rdp2/v5.5/rankinfo.do?groupcode=365905/365918/469202/614523&pageno=1&ua=Android_sst&version=4.2280",
            "http://218.200.160.29/rdp2/v5.5/rankinfo.do?groupcode=365905/365918/469202/2782650&pageno=1&ua=Android_sst&version=4.2280",
            "http://218.200.160.29/rdp2/v5.5/rankinfo.do?groupcode=365905/365918/469202/2784353&pageno=1&ua=Android_sst&version=4.2280"
    };




    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mainActivity = (MainActivity) context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mp_ranking_list, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView = (ListView) view.findViewById(R.id.list_view_ranking_fragment);

        listView.setOnItemClickListener(this);

        myApplication = (MyApplication) mainActivity.getApplication();

        getData();
    }

    @Override
    public void onResume() {
        super.onResume();


    }



    /**
     * 获取网络数据
     */
    private void getData() {

        VolleySingleton.addRequest(url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                GsonBeansRanking gsonBeansRanking = gson.fromJson(response, GsonBeansRanking.class);


                adapter = new RankingAdapter(mainActivity, gsonBeansRanking);
                listView.setAdapter(adapter);

                /**
                 *  向数据库中存储
                 * */

                nativeInformation.setName("ranking");
                nativeInformation.setContent(response);

                try {
                    NativeInformationBeans nativeInformationBeans =
                            myApplication.dbUtils.findFirst(Selector.from(NativeInformationBeans.class).where("name","=","ranking"));

                    if (null == nativeInformationBeans) {

                        myApplication.dbUtils.save(nativeInformation);
                    }else {
                        myApplication.dbUtils.update(nativeInformation,"content");
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(mainActivity, "RANKING_Error", Toast.LENGTH_SHORT).show();

                try {

                    NativeInformationBeans nativeInformationBeans =
                            myApplication.dbUtils.findFirst(Selector.from(NativeInformationBeans.class).where("name","=","ranking"));

                    gsonBeansRanking = gson.fromJson(nativeInformationBeans.getContent(),GsonBeansRanking.class);

                    adapter = new RankingAdapter(mainActivity,gsonBeansRanking);
                    listView.setAdapter(adapter);

                } catch (DbException e) {
                    e.printStackTrace();
                }
            }

        });

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        mainActivity.playService.setSkySoundListType(PlayService.RANKING_TYPE);

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        RankingDetailsFragment rankingDetailsFragment = RankingDetailsFragment.getInstance();


        bundle.putString("Url", urls[position]);

        /**
         * 为什么不写  rankingDetailsFragment = null;
         * 此处会报错
         * */
        rankingDetailsFragment.setArguments(bundle);

        transaction.add(R.id.fragment_group_ranking_details, rankingDetailsFragment);
        transaction.commit();



    }

    @Override
    public void onClick(View v) {
        //linearLayout.setVisibility(View.VISIBLE);
//
//        FragmentManager fragmentManager = getFragmentManager();
//        FragmentTransaction transaction = fragmentManager.beginTransaction();
//
//        RankingDetailsFragment rankingDetailsFragment = new RankingDetailsFragment();
//
//        transaction.add(R.id.fragment_group_ranking_details, rankingDetailsFragment);
//        transaction.commit();
//
//        Toast.makeText(getActivity(), "aa", Toast.LENGTH_SHORT).show();
    }
}
